﻿Imports System.ComponentModel
Imports SASettings

<ToolboxItem(False)>
Public Class ucInstall
    Private mSQL As Boolean
    Private Sub me_Load(sender As Object, e As EventArgs) Handles Me.Load
        DisplayMessage("")
        DisplayProgress("")

        lblDirectory.Text = gUpdateFiles.AppDirectory
        lblSQLMessage.Visible = False

        If gUpdateFiles.CheckDownloaded.Result <> SACommon.SAEventArgs.Results.Success Then
            MsgBox("All the required files did not download successfully." & vbCrLf & "All affected files will be redownloaded.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Download Error")
            gApp.ChangePage(MyApp.ApplicationType.DownloadFiles, True)
            Exit Sub
        End If

        If MyApp.IsUpdate = True Then
            'chkSQLServer.Checked = StartupSettings.SQLServerInstallation
            btnAction.Text = "Update"
            lblVersion.Text = String.Format("Update " & MyApp.ApplicationTitle & " to version {0} {1}", gUpdateVersion.NewApplicationVersion, gUpdateVersion.NewType)
            btnAction.Enabled = True
            chkSQLServer.Enabled = False
            Application.DoEvents()
            DoInstall()
        Else
            lblVersion.Text = String.Format("Install " & MyApp.ApplicationTitle & " {0} {1}", gUpdateVersion.NewApplicationVersion, gUpdateVersion.NewType)
            btnAction.Enabled = True
        End If
    End Sub
    Sub DoInstall()
        chkSQLServer.Enabled = False
        lblSQLMessage.Visible = False
        btnAction.Enabled = False

        AddHandler gUpdateFiles.MessageChanged, AddressOf DisplayMessage
        AddHandler gUpdateFiles.ProgressChanged, AddressOf DisplayProgress

        Try
            Dim tResult = gUpdateFiles.Install(5)
            If tResult.Result = SACommon.SAEventArgs.Results.Success Then

                DisplayMessage("Saving Startup Settings.")
                SetStartupSettings()

                If mSQL = True Then
                    gApp.ChangePage(MyApp.ApplicationType.SQLInstall, True)
                Else
                    gApp.ChangePage(MyApp.ApplicationType.InstallEnd, True)
                End If
            Else
                Throw New Exception(tResult.Message)
            End If
        Catch ex As Exception
            DisplayProgress(ex.Message)
            DisplayMessage("Please contact support.")
        End Try

        RemoveHandler gUpdateFiles.MessageChanged, AddressOf DisplayMessage
        RemoveHandler gUpdateFiles.ProgressChanged, AddressOf DisplayProgress
    End Sub
    Sub DisplayMessage(Message As String)
        lblMessage.Text = Message
        Application.DoEvents()
    End Sub
    Sub DisplayProgress(Message As String)
        lblProgress.Text = Message
        Application.DoEvents()
    End Sub

#Region "Startup Options"
    Sub SetStartupSettings()
        For Each item In gNewSettings.ValueList
            gUpdateSettings.Add(item.Key, item.Value)
        Next
        gUpdateSettings.Save()
    End Sub
#End Region
#Region "Process UI"
    Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
        DoInstall()
    End Sub
    Private Sub chkSQLServer_CheckedChanged(sender As Object, e As EventArgs) Handles chkSQLServer.CheckedChanged
        mSQL = chkSQLServer.Checked
        lblSQLMessage.Visible = chkSQLServer.Checked
    End Sub
#End Region

End Class
